package com.tencent50.easy;

import java.util.Stack;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 有效的括号
 * @date 2022/7/23 16:17
 */
public class Demo03 {
    public static void main(String[] args) {
        System.out.println(isValid("()"));

    }
    public static boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)==')'){
                if(stack.isEmpty()||stack.pop()!='('){
                    return false;
                }
            }
            else if(s.charAt(i)=='}'){
                if(stack.isEmpty()||stack.pop()!='{'){
                    return false;
                }
            }
            else if(s.charAt(i)==']'){
                if(stack.isEmpty()||stack.pop()!='['){
                    return false;
                }
            }else {
                stack.push(s.charAt(i));
            }
        }
        return stack.isEmpty();
    }
}
